package org.szgb.console.sys.custom.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.custom.entity.SysCustomFormModel;
import org.szgb.console.sys.custom.mapper.SysCustomFormModelMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysCustomFormModelService {
    private final SysCustomFormModelMapper sysCustomFormModelMapper;

    @Autowired
    public SysCustomFormModelService(SysCustomFormModelMapper sysCustomFormModelMapper){
        this.sysCustomFormModelMapper = sysCustomFormModelMapper;
    }

    @Transactional(readOnly = true)
    public SysCustomFormModel get(String id) {
        return sysCustomFormModelMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysCustomFormModel> findList(SysCustomFormModel sysCustomFormModel) {
        if (sysCustomFormModel != null && sysCustomFormModel.getPageDto() != null) {
            Page<SysCustomFormModel> page = new Page<>(sysCustomFormModel.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysCustomFormModel.setPage(page);
        }
        return sysCustomFormModelMapper.findList(sysCustomFormModel);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysCustomFormModel sysCustomFormModel) {
        return sysCustomFormModelMapper.getCount(sysCustomFormModel);
    }

    @Transactional()
    public void save(SysCustomFormModel sysCustomFormModel) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysCustomFormModel.getId() == null || sysCustomFormModel.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysCustomFormModel.setCreatedBy(sysCustomFormModel.getUpdatedBy());
            // 添加UUID
            sysCustomFormModel.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysCustomFormModelMapper.insert(sysCustomFormModel);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysCustomFormModelMapper.update(sysCustomFormModel);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysCustomFormModelMapper.delete(ids.split(","));
    }
}
